package com.example.phone.account.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.widget.ImageView;

import com.example.phone.R;
import com.example.phone.account.adapter.OrderRecycleAdapter;
import com.example.phone.base.BaseActivity;
import com.example.phone.base.MyApp;
import com.example.phone.base.RequestInterface;
import com.example.phone.base.ServerResponse;
import com.example.phone.bean.Order;
import com.example.phone.bean.Orders;
import com.example.phone.bean.User;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class WaitPayActivity extends BaseActivity {


    private static final int SEARCH_ORDER_SUCCESS = 1;
    @BindView(R.id.recylerView_order)
    RecyclerView recylerView;
    RecyclerView.Adapter adapter;

    User user;

    /**
     * 订单ID
     */
    List<Order> orders = new ArrayList<>();

    Handler handler = new Handler() {
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case SEARCH_ORDER_SUCCESS:

                    if (adapter == null) {

                        adapter = new OrderRecycleAdapter(getContext(), orders);
                        //log("handler =======  " + orders.get(0).getCurrentUnitPrice());

                        recylerView.setAdapter(adapter);
                    } else {
                        adapter.notifyDataSetChanged();
                    }

                    break;
            }

        }
    };
    @BindView(R.id.go_back)
    ImageView goBack;

    @Override
    public void initData() {


    }

    @Override
    public void initView() {
        ButterKnife.bind(this);
        MyApp myApp = (MyApp)getApplication();
        user = myApp.getUser();
        searchWaitPayProduct(user.getId());
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        recylerView.setLayoutManager(layoutManager);
        log("------------------执行完initView()----------");
    }

    private void searchWaitPayProduct(int userId) {

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(RequestInterface.basePath)
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        RequestInterface.OrderDao requestInterfaceOrderDao = retrofit.create(RequestInterface
                .OrderDao.class);

        Call call = requestInterfaceOrderDao
                .searchOrder(userId);


        call.enqueue(new Callback<ServerResponse<Orders>>() {
            @Override
            public void onResponse(Call<ServerResponse<Orders>> call, Response<ServerResponse<Orders>> response) {
                if (response.body() == null) {
                    toastShort("网络异常，" +
                            "请检查网络配置");

                    return;
                }

                if (response.body().getStatus() == 0) {
                    orders = response.body().getData().getList();


//                    for (OrderItem orderItem : orderItems) {
//                        log("--------------------打印--------------");
//                        log(orderItem.toString());
//                    }


//                    log(orderItems.get(0).getName());
                    toastShort("获取订单数据成功！");
                    log("获取订单数据成功！");
                    handler.sendEmptyMessage(SEARCH_ORDER_SUCCESS);


                } else {
                    toastShort("获取订单数据失败");
                    log("onRespnse: 获取订单数据失败");
                }

            }

            @Override
            public void onFailure(Call<ServerResponse<Orders>> call, Throwable t) {
                Log.d(TAG, "onFailure: ");
                toastShort("获取订单数据失败");
                log("onFailure: 获取数据失败");
            }
        });
    }

    @Override
    public int initLayout() {
        return R.layout.activity_wait_paying;
    }




    @OnClick(R.id.go_back)
    public void onViewClicked() {
        goNextActivity(TestActivity.class);
        finish();
    }
}
